package com.anyonavinfo.cpaddemo.view.mainview;


import android.util.Log;

import com.anyonavinfo.cpaddemo.data.appinfo.source.AppInfoDataSource;
import com.anyonavinfo.cpaddemo.data.appinfo.source.AppInfoRepository;


/**
 * Created by ZuiC on 2017/1/16.
 */

public class MainViewPresenter implements MainViewContract.Presenter {

    private static final String TAG = "OuterFramePresenter";
    private final MainViewContract.View mOuterFrameView;
    private final AppInfoRepository mAppInfoRepository;

    public MainViewPresenter(MainViewContract.View view, AppInfoRepository appInfoRepository){
        this.mOuterFrameView = view;
        this.mAppInfoRepository = appInfoRepository;
        mOuterFrameView.setPresenter(this);
    }


    @Override
    public void start() {
        loadAppInfo(false);
    }

    private void loadAppInfo(){
        mAppInfoRepository.loadAppInfo(new AppInfoDataSource.LoadAppInfoCallBack() {
            @Override
            public void getInstalledAppNum(int num) {
                int size = num / 9 + (num % 9 != 0 ? 1 : 0) + 1;
                mOuterFrameView.updatePageSize(size);
                mOuterFrameView.updateDotIconNum();
                mOuterFrameView.updateDotIconState(0);
                Log.e(TAG, size + "");
            }
        });
    }

    @Override
    public void onPageSelected(int position) {
        mOuterFrameView.updateDotIconState(position);
    }

    @Override
    public void openASRView() {

    }

    @Override
    public void loadAppInfo(boolean reloadAppInfo) {
        if (reloadAppInfo){
            mAppInfoRepository.reloadAppInfo();
        }
        loadAppInfo();
    }
}
